{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# aboutExcept"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutException/notice.py\n",
    "print(bool(0))\n",
    "print(bool(-1))\n",
    "print(bool(0.1))\n",
    "print(bool(0.0))\n",
    "print(bool(''))\n",
    "print(bool('vd'))\n",
    "print(bool([]))\n",
    "print(bool([1]))\n",
    "print(bool(()))\n",
    "print(bool((1,2)))\n",
    "      \n",
    "print(bool({}))\n",
    "print(bool({1:1}))\n",
    "\n",
    "if []:\n",
    "    print('True')\n",
    "else:\n",
    "    print('False')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 try...except"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutException/noexception.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutException/noexception.py\n",
    "def divide(a, b):\n",
    "    return a/b\n",
    "while True:\n",
    "    sFirst = input(\"First number:\")\n",
    "    if sFirst == \"q\": \n",
    "        break\n",
    "    sSecond = input(\"Second number:\")\n",
    "    if sSecond == \"q\":\n",
    "        break\n",
    "    iFirst = int(sFirst)\n",
    "    iSecond = int(sSecond)\n",
    "    fResult = divide(iFirst,iSecond)\n",
    "    print('{}/{}={}'.format(iFirst,iSecond,fResult))\n",
    "print('bye')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%%writefile aboutException/tryexcept.py\n",
    "def divide(a, b):\n",
    "    return a/b\n",
    "while True:\n",
    "    sFirst = input(\"First number:\")\n",
    "    if sFirst == \"q\": \n",
    "        break\n",
    "    sSecond = input(\"Second number:\")\n",
    "    if sSecond == \"q\":\n",
    "        break\n",
    "    try:\n",
    "        iFirst = int(sFirst)\n",
    "        iSecond = int(sSecond)\n",
    "        fResult = divide(iFirst,iSecond)\n",
    "    except (ZeroDivisionError) as e:\n",
    "        print(\"You can not divide by 0:\",e)\n",
    "        \n",
    "    except (ValueError,TypeError) as e:\n",
    "        print(\"Illegal value been inputted:\",e)\n",
    "        print(type(e))\n",
    "    except (Exception) as e:\n",
    "        print(\"An exception found, I do not know how to process it.\")\n",
    "        raise\n",
    "    else:\n",
    "        print( \"I am else statement.\",sFirst, \"/\", sSecond, \"=\", fResult)\n",
    "    finally:\n",
    "        print(\"I am finally statement. 'Finally' will be executed what ever happens.\")\n",
    "print('bye')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 2 assert\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutException/assert.py\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutException/assert.py\n",
    "\n",
    "a = 100\n",
    "assert(a == 100)\n",
    "print('bye')\n",
    "\n",
    "a = 10\n",
    "def temp(para):\n",
    "    if para != True:\n",
    "        raise(AssertionError)\n",
    "temp(a==100)    \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 unittest\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import unittest\n",
    "#dir(unittest)\n",
    "#print(help(unittest.TestCase))\n",
    "for i in range(2,2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutException/add.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutException/add.py\n",
    "def add(a,b):\n",
    "    return a+b\n",
    "def sun(a,b):\n",
    "    return a-b\n",
    "def isPrime(n):\n",
    "    for i in range(2,n):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "if __name__ == '__main__':\n",
    "    print(isPrime(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "test (__main__.testDemo) ... FAIL\n",
      "test_add (__main__.testDemo) ... ok\n",
      "test_sub (__main__.testDemo) ... ok\n",
      "\n",
      "======================================================================\n",
      "FAIL: test (__main__.testDemo)\n",
      "----------------------------------------------------------------------\n",
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-1-81fe6e3eb268>\", line 17, in test\n",
      "    self.assertEqual(isPrime(-7),False,'负数不是素数')\n",
      "AssertionError: True != False : 负数不是素数\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 3 tests in 0.004s\n",
      "\n",
      "FAILED (failures=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<unittest.main.TestProgram at 0x7fa7bcde7a90>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#%%writefile aboutException/utExample.py\n",
    "from aboutException.add import *\n",
    "import unittest\n",
    "class testDemo(unittest.TestCase):\n",
    "    def test_add(self):\n",
    "        self.assertEqual(2,add(1,1))\n",
    "        self.assertEqual(10,add(9,1))\n",
    "        self.assertEqual(11,add(9,2))\n",
    "    def test_sub(self):\n",
    "        self.assertEqual(2,add(1,1))        \n",
    "    def test(self):\n",
    "        self.assertEqual(isPrime(2),True,'素数判断错误')\n",
    "        self.assertEqual(isPrime(7),True,'素数判断错误')\n",
    "        self.assertEqual(isPrime(12),False,'12不是素数，判断错误')\n",
    "        self.assertEqual(isPrime(0),False,'0不是素数，判断错误')\n",
    "        self.assertEqual(isPrime(1),False,'1不是素数，判断错误')\n",
    "        self.assertEqual(isPrime(-7),False,'负数不是素数')\n",
    " \n",
    "unittest.main(verbosity = 2,argv=['first-arg-is-ignored'],exit=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'type'>\n"
     ]
    }
   ],
   "source": [
    "import unittest\n",
    "dir(unittest)\n",
    "print(type(unittest.TestCase))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
